<template>
  <div style="display:flex;justify-content:center;align-items: center;height: 100vh;background: #0f9876">
    <div style="width: 50%;display: flex;background: #fff;border-radius: 10px;overflow: hidden">
      <img src="../assets/login.png" alt=""/>
      <div style="width: 50%">
        <div style="font-size: 20px;font-weight: bold;text-align: center;padding: 30px">欢迎登录后台管理系统</div>
        <el-form :model="user" :rules="rules" ref="userForm">
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" v-model="user.username" placeholder="请输入账号"/>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-circle-check" show-password v-model="user.password"
                      placeholder="请输入密码"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%" @click="login">登录</el-button>
          </el-form-item>
          <div style="display: flex;justify-content: space-between;padding: 0 50px">
            <div>还没有账号? 请<span style="color:#0f9867;cursor: pointer">注册</span></div>
            <div><span style="color:#0f9867;cursor: pointer">忘记密码</span></div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>

import {loginApi} from "@/Api/userApi";

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Login",
  data() {
    return {
      user: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        password: [{required: true, message: '请输入密码', trigger: 'blur'}]
      },
    }
  },
  methods: {
    login() {
      this.$refs["userForm"].validate(async (valid) => {
        if (valid) {
          const res = await loginApi('post',this.user)
          if(res['code'] === '200'){
            this.$message.success('登录成功')
            await this.$router.push('/')
          }else{
            this.$message.error(res['message'])
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  },
}
</script>

<style scoped>

</style>
